package org.loofer.ext.utils;

import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public final class ArchiveUtils {
    private static final int BUFFER_SIZE = 2048;

    private ArchiveUtils() {
    }

    public static boolean zip(File src, File dst) {
        return zip(src, dst, null);
    }

    public static boolean zip(File src, File dst, String entryName) {
        Throwable th;
        if (src == null || dst == null || !src.exists()) {
            return false;
        }
        Closeable ins = null;
        Closeable ous = null;
        try {
            Closeable ins2 = new FileInputStream(src);
            try {
                Closeable ous2 = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dst)));
                try {
                    byte[] buffer = new byte[2048];
                    if (entryName == null) {
                        entryName = src.getName();
                    }
                    zip(entryName, (InputStream) ins2, (ZipOutputStream) ous2, buffer);
                    IoUtils.closeSilently(ins2);
                    IoUtils.closeSilently(ous2);
                    return true;
                } catch (Throwable th2) {
                    th = th2;
                    ous = ous2;
                    ins = ins2;
                    IoUtils.closeSilently(ins);
                    IoUtils.closeSilently(ous);
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                ins = ins2;
                IoUtils.closeSilently(ins);
                IoUtils.closeSilently(ous);
                throw th;
            }
        } catch (Throwable th4) {
            FileUtils.delete(dst);
            IoUtils.closeSilently(ins);
            IoUtils.closeSilently(ous);
            return false;
        }
    }

    public static boolean zip(File[] src, File dst) {
        return zip(src, dst, null);
    }

    public static boolean zip(File[] src, File dst, FileFilter srcFilter) {
        return zip(src, dst, null, srcFilter);
    }

    public static boolean zip(File[] src, File dst, Comparator<File> srcSorter, FileFilter srcFilter) {
        Throwable th;
        if (src == null || dst == null) {
            return false;
        }
        if (srcSorter != null) {
            Arrays.sort(src, 0, src.length, srcSorter);
        }
        Closeable closeable = null;
        try {
            Closeable ous = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dst)));
            try {
                byte[] buffer = new byte[2048];
                for (File file : src) {
                    if (srcFilter == null || srcFilter.accept(file)) {
                        Closeable ins = null;
                        try {
                            Closeable ins2 = new FileInputStream(file);
                            try {
                                zip(file.getName(), (InputStream) ins2, (ZipOutputStream) ous, buffer);
                                IoUtils.closeSilently(ins2);
                            } catch (Throwable th2) {
                                th = th2;
                                ins = ins2;
                            }
                        } catch (Throwable th3) {
                            th = th3;
                        }
                    }
                }
                IoUtils.closeSilently(ous);
                return true;
            } catch (Throwable th4) {
                th = th4;
                closeable = ous;
            }
        } catch (Throwable th5) {
            try {
                FileUtils.delete(dst);
                IoUtils.closeSilently(closeable);
                return false;
            } catch (Throwable th6) {
                Throwable th7;
                th7 = th6;
                IoUtils.closeSilently(closeable);
                throw th7;
            }
        }
        IoUtils.closeSilently(ins);
        throw th;
    }

    private static void zip(String entry, InputStream ins, ZipOutputStream ous, byte[] buffer) throws IOException {
        if (buffer == null) {
            buffer = new byte[2048];
        }
        ous.putNextEntry(new ZipEntry(entry));
        while (true) {
            int count = ins.read(buffer);
            if (count > 0) {
                ous.write(buffer, 0, count);
            } else {
                return;
            }
        }
    }
}
